<?php 
/**
 * 数据分页
 *
 * @category   Custom
 * @package    Custom_Model 
 * @Contributed by Gangther
 */
 
class PageNav
{
	/**
     * 分页大小
     * 
     * @var    int
     */
	private $_pageSize = 20;
	
	function page($total=0,$pageid=0,$psize=0,$halfPage=5)
	{
	    parse_str($_SERVER['QUERY_STRING'], $_GET); 
		
		foreach($_GET as $key => $val){ 
		    if($key != 'pageid'){
			    $query .= $key.'='.$val.'&';
			}
			
		} 
		
	    $url = $_SERVER['PATH_INFO'].'?'.$query; 
 
        $psize = ((int)$psize > 0) ? (int)$psize : $this -> _pageSize;
 
		$totalPage = intval($total/$psize);
	
		if($total%$psize) { 
			$totalPage++;  
		}
	 
		if($totalPage<2) { 
			return false; 
		}
	 
		if(empty($pageid)) { 
			$pageid = 1; 
		}
	 
		if($pageid > $totalPage) { 
			$pageid = $totalPage;
	
		}
	 
		$array_m = 0;
	
		if($pageid > 0)  { 
			$returnlist[$array_m]["url"] = $url; 
			$returnlist[$array_m]["name"] = "首页"; 
			$returnlist[$array_m]["status"] = 0; 
			if($pageid > 1) { 
				$array_m++; 
				$returnlist[$array_m]["url"] = $url."pageid=".($pageid-1); 
				$returnlist[$array_m]["name"] = "上一页"; 
				$returnlist[$array_m]["status"] = 0; 
			} 
		}
	 
		for($i=$pageid-$halfPage,$i>0 || $i=0,$j=$pageid+$halfPage,$j<$totalPage || $j=$totalPage;$i<$j;$i++) { 
			$l = $i + 1; 
			$array_m++; 
			$returnlist[$array_m]["url"] = $url."pageid=".$l; 
			$returnlist[$array_m]["name"] = $l; 
			$returnlist[$array_m]["status"] = ($l == $pageid) ? 1 : 0; 
		}
 
		if($pageid < $totalPage)  { 
			$array_m++; 
			$returnlist[$array_m]["url"] = $url."pageid=".($pageid+1); 
			$returnlist[$array_m]["name"] = "下一页"; 
			$returnlist[$array_m]["status"] = 0; 
		}
	
		$array_m++; 
		$returnlist[$array_m]["url"] = $url."pageid=".$totalPage; 
		$returnlist[$array_m]["name"] = "末页"; 
		$returnlist[$array_m]["status"] = 0; 
		
		foreach($returnlist AS $key=>$value) { 
			if($value["status"]) { 
				$msg .= "<strong>".$value["name"]."</strong>"; 
			} else { 
				$msg .= "<a href='".$value["url"]."'>".$value["name"]."</a>"; 
			} 
		} 
		unset($returnlist); 
		return $msg;
	
	} 

}